// 跨表条件

// 生成一个随机的id
export const generateRandomId = () => {
  // 使用 Date.now() 获取当前时间戳
  const timestamp = Date.now()

  // 使用 Math.random() 生成一个随机数并转换为字符串
  const randomStr = Math.random().toString(36).substring(2, 8)

  // 将时间戳和随机数拼接起来作为ID
  const randomId = timestamp.toString() + randomStr

  return randomId
}

/* 
1. 点击增加条件
*/
export const btnCrossConditionAddConPublic = crossObjArrValue => {
  if (crossObjArrValue.length === 0) {
    crossObjArrValue.push({
      interview: '',
      form: '',
      word: '',
      firstSelect: '==',
      secondSelect: '与变量比较',
      interviewTwo: '',
      formTwo: '',
      wordTwo: '',
      third: '',
      fourth: '数值',
      formOptions: [],
      wordOptions: [],
      formOptionsTwo: [],
      wordOptionsTwo: [],
      fifth: '',
      fifthOptions: [],
      variableType: '',
      id: generateRandomId(),
    })
  } else {
    crossObjArrValue.push({
      interview: '',
      form: '',
      word: '',
      firstSelect: '==',
      secondSelect: '与变量比较',
      interviewTwo: '',
      formTwo: '',
      wordTwo: '',
      third: '',
      fourth: '数值',
      formOptions: [],
      wordOptions: [],
      formOptionsTwo: [],
      wordOptionsTwo: [],
      id: generateRandomId(),
      zero: '&&',
    })
  }
  return crossObjArrValue
}

/* 
2. 点击确认
*/
export const btnTableConditionConfirmCrossPublic = (
  crossObjArrValue,
  wordOptionsEnums,
  tableConditionOptionsZeroEnums,
  tableConditionOptionsTwoEnums,
  interviewOptionseEnums,
  fifthOptionsEnums
) => {
  console.log(fifthOptionsEnums, 'fifthOptionsEnums ')
  // 2.1 一个参数
  let paramsString = ''
  // 2.2 二个参数
  let paramsStringTwo = ''
  crossObjArrValue.forEach(item => {
    const formOptionsEnums = {}
    const formOptionsTwoEnums = {}
    if (item.formOptions) {
      item.formOptions.forEach(item => {
        formOptionsEnums[item.tableCode] = item.tableName
      })
    }
    if (item.formOptionsTwo) {
      item.formOptionsTwo.forEach(item => {
        formOptionsTwoEnums[item.tableCode] = item.tableName
      })
    }
    if (item.zero) {
      // 非首次
      if (item.firstSelect === '为空' || item.firstSelect === '不为空') {
        if (item.firstSelect === '为空') {
          paramsString =
            paramsString +
            ` ${item.zero} (${item.interview}.${item.form}.${item.word} == 'null' || ${item.form}.${item.word} == '')`

          paramsStringTwo =
            paramsStringTwo +
            ` ${tableConditionOptionsZeroEnums[item.zero]} ${interviewOptionseEnums[item.interview]}.${
              formOptionsEnums[item.form]
            }.${wordOptionsEnums[item.word]} 为空`
        } else if (item.firstSelect === '不为空') {
          paramsString =
            paramsString +
            ` ${item.zero} (${item.interview}.${item.form}.${item.word} != 'null' || ${item.form}.${item.word} != '')`
          paramsStringTwo =
            paramsStringTwo +
            ` ${tableConditionOptionsZeroEnums[item.zero]} ${interviewOptionseEnums[item.interview]}.${
              formOptionsEnums[item.form]
            }.${wordOptionsEnums[item.word]} 不为为空`
        }
      }
      if (item.secondSelect === '与变量比较') {
        paramsString =
          paramsString +
          ` ${item.zero} ${item.interview}.${item.form}.${item.word} ${item.firstSelect} ${item.interviewTwo}.${item.formTwo}.${item.wordTwo} `
        paramsStringTwo =
          paramsStringTwo +
          ` ${tableConditionOptionsZeroEnums[item.zero]} ${interviewOptionseEnums[item.interview]}.${
            formOptionsEnums[item.form]
          }.${wordOptionsEnums[item.word]} ${tableConditionOptionsTwoEnums[item.firstSelect]} ${
            interviewOptionseEnums[item.interviewTwo]
          }.${formOptionsTwoEnums[item.formTwo]}.${wordOptionsEnums[item.wordTwo]}`
      } else if (item.secondSelect === '与具体值比较') {
        if (item.fourth === '数值') {
          paramsString =
            paramsString + ` ${item.zero} ${item.interview}.${item.form}.${item.word} ${item.firstSelect} ${this.third}`
          paramsStringTwo =
            paramsStringTwo +
            ` ${tableConditionOptionsZeroEnums[item.zero]} ${interviewOptionseEnums[item.interview]}.${
              formOptionsEnums[item.form]
            }.${wordOptionsEnums[item.word]} ${tableConditionOptionsTwoEnums[item.firstSelect]} ${this.third}`
        } else if (item.fourth === '字符') {
          paramsString =
            paramsString +
            ` ${item.zero} ${item.interview}.${item.form}.${item.word} ${item.firstSelect} '${this.third}'`
          paramsStringTwo =
            paramsStringTwo +
            ` ${tableConditionOptionsZeroEnums[item.zero]} ${interviewOptionseEnums[item.interview]}.${
              formOptionsEnums[item.form]
            }.${wordOptionsEnums[item.word]} ${tableConditionOptionsTwoEnums[item.firstSelect]} '${this.third}'`
        }
      }
    } else {
      // 首次
      if (item.firstSelect === '为空' || item.firstSelect === '不为空') {
        if (item.firstSelect === '为空') {
          paramsString =
            paramsString + `(${item.interview}.${item.form}.${item.word} == 'null' || ${item.form}.${item.word} == '')`
          paramsStringTwo =
            paramsStringTwo +
            `${interviewOptionseEnums[item.interview]}.${formOptionsEnums[item.form]}.${
              wordOptionsEnums[item.word]
            } 为空`
        } else if (item.firstSelect === '不为空') {
          paramsString =
            paramsString + `(${item.interview}.${item.form}.${item.word} != 'null' || ${item.form}.${item.word} != '')`
          paramsStringTwo =
            paramsStringTwo +
            `${interviewOptionseEnums[item.interview]}.${formOptionsEnums[item.form]}.${
              wordOptionsEnums[item.word]
            } 不为为空`
        }
      } else {
        if (item.secondSelect === '与变量比较') {
          paramsString =
            paramsString +
            `${item.interview}.${item.form}.${item.word} ${item.firstSelect} ${item.interviewTwo}.${item.formTwo}.${item.wordTwo}`
          paramsStringTwo =
            paramsStringTwo +
            `${interviewOptionseEnums[item.interview]}.${formOptionsEnums[item.form]}.${wordOptionsEnums[item.word]} ${
              tableConditionOptionsTwoEnums[item.firstSelect]
            } ${interviewOptionseEnums[item.interviewTwo]}.${formOptionsTwoEnums[item.formTwo]}.${
              wordOptionsEnums[item.wordTwo]
            }`
        } else if (item.secondSelect === '与具体值比较') {
          if (item.variableType === '1020') {
            // 英文
            paramsString =
              paramsString + `${item.interview}.${item.form}.${item.word} ${item.firstSelect} ${item.fifth}`
            // 中文
            paramsStringTwo =
              paramsStringTwo +
              `${interviewOptionseEnums[item.interview]}.${formOptionsEnums[item.form]}.${
                wordOptionsEnums[item.word]
              } ${tableConditionOptionsTwoEnums[item.firstSelect]} ${fifthOptionsEnums[item.fifth]}`
          } else {
            if (item.fourth === '数值') {
              paramsString =
                paramsString + `${item.interview}.${item.form}.${item.word} ${item.firstSelect} ${item.third}`
              paramsStringTwo =
                paramsStringTwo +
                `${interviewOptionseEnums[item.interview]}.${formOptionsEnums[item.form]}.${
                  wordOptionsEnums[item.word]
                } ${tableConditionOptionsTwoEnums[item.firstSelect]} ${item.third}`
            } else if (item.fourth === '字符') {
              paramsString =
                paramsString + `${item.interview}.${item.form}.${item.word} ${item.firstSelect} '${item.third}'`
              paramsStringTwo =
                paramsStringTwo +
                `${interviewOptionseEnums[item.interview]}.${formOptionsEnums[item.form]}.${
                  wordOptionsEnums[item.word]
                } ${tableConditionOptionsTwoEnums[item.firstSelect]} '${item.third}'`
            }
          }
        }
      }
    }
  })
  return {
    paramsString,
    paramsStringTwo,
  }
}
